<template>
  <div class="home">
    <div class="sideBar" :class="{ sideBarActive: this.isCollapse }">
      <div class="title">{{ title }}</div>
      <SvgIcon class="toggle" :iconClass="iconName" @click="handleClick" />
      <div class="sideBarCont">
        <MenuComponent :isCollapse="isCollapse" />
      </div>
    </div>
    <div class="cont" :class="{ contActive: this.isCollapse }">
      <div class="header">
        <HeadComponent />
      </div>
      <div class="tabRoute">
        <el-scrollbar>
          <div class="scrollbar-flex-content">
            <p
              v-for="(item, index) in tab_routes"
              :key="index"
              class="scrollbar-demo-item"
              :class="{ active: item.isSelect }"
            >
              <router-link :to="item.path">
                <span class="info">{{ item.name }}</span>
              </router-link>
              <span
                v-if="!item.isDefault"
                class="close"
                @click="handleRemoveRoute(item)"
                >×</span
              >
            </p>
          </div>
        </el-scrollbar>
      </div>
      <div class="body">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import MenuComponent from "@/components/menu";
import HeadComponent from "@/components/header";
import SvgIcon from "@/components/SvgIcon.vue";
import { mapGetters } from "vuex";
export default {
  name: "HomePage",
  components: { MenuComponent, HeadComponent, SvgIcon },
  computed: {
    ...mapGetters(["tab_routes"]),
  },
  data() {
    return {
      isCollapse: false,
      title: "VUE-ADMIN-MS",
      iconName: "shousuo",
      tabRouteLists: {},
    };
  },
  methods: {
    handleClick() {
      this.isCollapse = !this.isCollapse;
      this.title = this.isCollapse ? "MS" : "VUE-ADMIN-MS";
      this.iconName = this.isCollapse ? "zhankai" : "shousuo";
    },
    // 删除当前导航标签
    handleRemoveRoute(item) {
      this.$store.dispatch("route/removeTabRoutes", item).then(() => {
        const list = this.tab_routes.find((ele) => ele.isSelect);
        if (!list) {
          const len = this.tab_routes.length;
          this.$router.push({ path: this.tab_routes[len - 1].path });
        }
      });
    },
  },
};
</script>

<style scoped>
.home {
  width: 100%;
  height: 100%;
  background: #f2f5fa;
  position: relative;
  overflow: hidden;
}

.home .sideBar {
  width: 210px;
  height: 100%;
  transition: width 0.3s;
  background: rgb(32, 112, 165);
  box-shadow: 5px 0px 5px rgb(240, 233, 233);
  box-sizing: border-box;
  position: absolute;
  color: #fff;
  top: 0;
  left: 0;
}

.home .sideBarActive {
  width: 65px !important;
}

.home .sideBar .title {
  height: 60px;
  line-height: 60px;
  font-size: 20px;
  padding: 0 20px;
  overflow: hidden;
}

.home .sideBar .toggle {
  width: 35px;
  height: 35px;
  position: absolute;
  top: 13px;
  right: -50px;
  color: rgba(51, 51, 51, 0.8);
  cursor: pointer;
}

.home .sideBar .toggle:hover {
  color: rgba(51, 51, 51, 1);
}

.home .sideBarCont {
  width: 100%;
  height: calc(100% - 60px);
  box-shadow: 5px 0px 5px rgb(240, 233, 233);
  overflow-x: hidden;
}

.home .cont {
  width: calc(100% - 210px);
  height: 100%;
  /* 右侧Dom元素右浮动，目的在于解决宽度改变，内容闪动的情况。 */
  float: right;
  margin-left: 210px;
  transition: width 0.3s;
  box-sizing: border-box;
}
.home .contActive {
  width: calc(100% - 65px);
  margin-left: 65px;
}

.home .cont .header {
  width: 100%;
  height: 60px;
  background: #fff;
  box-shadow: 5px 5px 5px rgb(240, 233, 233);
}
.home .cont .body {
  width: 100%;
  height: calc(100% - 110px);
  padding: 24px;
  box-sizing: border-box;
  overflow-y: auto;
}

.tabRoute {
  width: 100%;
  height: 50px;
  box-sizing: border-box;
  border-bottom: 1px solid rgb(240, 233, 233);
  box-shadow: 5px 5px 5px rgb(240, 233, 233);
}
.scrollbar-flex-content {
  display: flex;
}
.scrollbar-demo-item {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 25px;
  margin: 10px;
  /* padding: 2px 20px; */
  text-align: center;
  border-radius: 4px;
  /* border: 1px solid rgba(32, 112, 165, 1); */
  border: 1px solid var(--theme-color);
  background: #fff;
  color: #333;
  cursor: pointer;
  position: relative;
}

.scrollbar-demo-item a {
  color: #333;
}

.scrollbar-demo-item .info {
  padding: 2px 24px;
  box-sizing: border-box;
}

.scrollbar-demo-item.active {
  /* background: rgba(32, 112, 165, 1); */
  background: var(--theme-color);
  color: #fff;
}

.scrollbar-demo-item.active a {
  color: #fff;
}

.scrollbar-demo-item:hover .close {
  display: block;
}

.scrollbar-demo-item .close {
  width: 15px;
  height: 15px;
  display: none;
  text-align: center;
  line-height: 15px;
  border-radius: 50px;
  /* background: rgba(32, 112, 165, 0.9); */
  background: var(--theme-color);
  color: #fff;
  position: absolute;
  right: -5px;
  top: -5px;
}
</style>
